package pub.wkq.innerclass;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.Instant;

public class InnerClassTest {
    public static void main(String[] args) {
        TalkingClock clock = new TalkingClock(1000, true);
        clock.start();

        JOptionPane.showMessageDialog(null,"Quit program?");
        System.exit(0);
    }
}

/**
 * 语音闹钟类
 */
class TalkingClock{
    private int interval;
    private boolean beep;

    public TalkingClock(int interval, boolean beep) {
        this.interval = interval;
        this.beep = beep;
    }
    public void start() {
//        var listener = new TimePrinter();
        //
        var listener = TalkingClock.this.new TimePrinter();
        var timer = new Timer(interval, listener);
        timer.start();
    }

    private class TimePrinter implements ActionListener {
//        private String text = "12";
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println(Instant.ofEpochMilli(e.getWhen()));
            System.out.println("beep = " + TalkingClock.this.beep);
//            System.out.println("text = " + this.text);
            if (beep) {
                Toolkit.getDefaultToolkit().beep();
            }
        }
    }
}

